package com.myandroid.custombookmark;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.DialogInterface;

import android.database.Cursor;

import android.os.Bundle;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class CustomBookmark extends ListActivity {
    /** Called when the activity is first created. */
	//private BookmarkDB		bookmarkDB;
	private SimpleCursorAdapter	adapter;
	private Cursor 				cursor;
	
	private static final int	MENU_ADD	= 0;
	private static final int	MENU_EDIT	= 1;
	private static final int	MENU_DELETE	= 2;
	
	private void init() {
		//cursor	= bookmarkDB.getBookmarks();
		cursor	= managedQuery(Bookmark.sites.CONTENT_URI, null, null, null, null);
		//startManagingCursor(cursor);
		String [] from 	= new String[] {Bookmark.sites.NAME, Bookmark.sites.ADDRESS};
		int[] to		= new int[] {R.id.row_name, R.id.row_address};
		
		adapter	= new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
		setListAdapter(adapter);
	}
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //bookmarkDB	= new BookmarkDB(this);
        registerForContextMenu(getListView());
    }
		
	@Override
	public void onResume() {
		super.onResume();
		//bookmarkDB.open();
		init();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		//bookmarkDB.close();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_ADD, Menu.NONE, "북마크 추가")
			.setIcon(android.R.drawable.ic_menu_add);
		return true;		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()) {
			case MENU_ADD :
				LayoutInflater li	= getLayoutInflater();
				final View v		= li.inflate(R.layout.edit, null);
				new AlertDialog.Builder(this)
						.setTitle("북마크 편집")
						.setView(v)
						.setPositiveButton("확인", new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog, int which) {
								EditText name		= (EditText)v.findViewById(R.id.edit_name);
								EditText address	= (EditText)v.findViewById(R.id.edit_address);
								ContentValues values = new ContentValues();
								values.put(Bookmark.sites.NAME, name.getText().toString());
								values.put(Bookmark.sites.ADDRESS, address.getText().toString());
								getContentResolver().insert(Bookmark.sites.CONTENT_URI, values);
								
								/*bookmarkDB.insertBookmark(name.getText().toString(), address.getText().toString());
									cursor.requery();
								 */								
							}
						})
						.setNegativeButton("취소", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								//Do Nothing
							}
						}).show();
				return true;
		}
		return false;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, MENU_EDIT,	 Menu.NONE, "수정");
		menu.add(0, MENU_DELETE, Menu.NONE, "삭제");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		
		switch(item.getItemId()) {
			case MENU_EDIT :
				LayoutInflater li		= getLayoutInflater();
				final View v			= li.inflate(R.layout.edit, null);
				final EditText name		= (EditText)v.findViewById(R.id.edit_name);
				final EditText address	= (EditText)v.findViewById(R.id.edit_address);
				
				//Cursor c = bookmarkDB.getBookmark(info.id);
				Cursor c = managedQuery(
						ContentUris.withAppendedId(Bookmark.sites.CONTENT_URI, info.id),
																	null, null, null, null);
				if(c != null && c.getCount() !=0) {
					c.moveToFirst();
				}
				
				String nameStr	= c.getString(c.getColumnIndex(Bookmark.sites.NAME));
				String addrStr	= c.getString(c.getColumnIndex(Bookmark.sites.ADDRESS));
				
				name.setText(nameStr);
				address.setText(addrStr);
				
				new AlertDialog.Builder(this)
					.setTitle("북마크 편집")
					.setView(v)
					.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							ContentValues values	= new ContentValues();
							values.put(Bookmark.sites.NAME, name.getText().toString());
							values.put(Bookmark.sites.ADDRESS, address.getText().toString());
							getContentResolver().update(
									ContentUris.withAppendedId(Bookmark.sites.CONTENT_URI, info.id),
																					values, null, null);
							/*
							 bookmarkDB.updateBookmark(info.id, name.getText().toString(),
							 							 		address.getText().toString());
							 cursor.request();
							*/
						}
					})
					.setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							//Do Nothing
						}
					}).show();
				return true;
			case MENU_DELETE :
				getContentResolver().delete(
						ContentUris.withAppendedId(Bookmark.sites.CONTENT_URI, info.id),
																				null,null);
				/*
				 bookmarkDB.deleteBookmark(info.id);
				 cursor.requery();
				 */
				return true;
		}
		return false;
	}
}